home *** CD-ROM | disk | FTP | other *** search
/ MacHack 1993 / MacHack 1993.toast / MacHack™ 1987-1992 / MacHack™ '87 / Source ƒ / C ƒ / SimpleDemo & LittleDemo / SimpleDemo.c < prev    next >
Encoding:
C/C++ Source or Header  |  1987-03-28  |  12.7 KB  |  394 lines  |  [TEXT/KAHL]

  1.  
  2. /*
  3.     Title    : SimpleDemo.c
  4.     Author    : Erik Kilk
  5.     Date    : June 9, 1985
  6.     Revised    : July 17, 1985, November 1986
  7.     
  8.     Synopsis: A demonstration of the use of SimpleTools with 
  9.         other toolbox calls.  This demonstrates a very 
  10.         advanced use of SimpleTools.  I've always disliked
  11.         being given simple examples.  So, this example 
  12.         uses many, many optional features of SimpleTools and
  13.         shows how you can use the standard toolbox calls.
  14.         
  15.     The SimpleTools routines are being freely distributed.
  16.     You may use them, distribute your applications which use them,
  17.     and copy them if you send $20 or more to:
  18.     
  19.             Erik Kilk
  20.             4949 Snyder Lane, #247
  21.             Rohnert Park,  CA  94928
  22.             
  23.     Link this demonstration program with SimpleTools.
  24.         
  25. */
  26.  
  27. /*********************************/
  28. /*  DEFINITIONS FOR SIMPLETOOLS  */
  29. /*********************************/
  30.  
  31. #include "simple.h"
  32.  
  33.  
  34. /*******************************/
  35. /*  INCLUDE FILES FOR TOOLBOX  */
  36. /*******************************/
  37.  
  38. #ifdef MEGAMAX
  39.   #include <qd.h>
  40.   #include <event.h>
  41.   #include <mem.h>
  42.   #include <font.h>
  43.   #include <qdvars.h>
  44.   #include <misc.h>
  45.   #include <win.h>
  46.   #include <string.h>
  47.   #include <stdio.h>
  48.   #define TRUE (-1)            /* Common boolean constants */
  49.   #define FALSE 0
  50. #else
  51.   #include <Quickdraw.h>
  52.   #include <EventMgr.h>
  53.   #include <MemoryMgr.h>
  54.   #include <fontMgr.h>
  55.   #include <WindowMgr.h>
  56.   #include <strings.h>
  57.   #include <stdio.h>
  58. #endif
  59.  
  60. /**********************/
  61. /*  GLOBAL VARIABLES  */
  62. /**********************/
  63.  
  64. int circspeed = 50;        /* rate of drawing new circles */
  65. int ourpensize = 1;        /* global pen size instead of windows */
  66. Rect drag;            /* for moving our free mem manually */
  67. char name[100];
  68. char outstring[100];
  69.  
  70.  
  71. /*********************/
  72. /*   Odds and Ends   */
  73. /*********************/
  74.  
  75. nop()                 /* Attached as a null pointer */
  76. {
  77. }            
  78.  
  79. /***********************/
  80. /*  Sketch Window      */
  81. /***********************/
  82.  
  83. sketactivate()            /* Attached to sketch's activate */
  84. {                /* Disable the whole sketch menu */
  85.   menu ("Sketch", "", itemenable);
  86.   menu ("Edit", "Clear", home);  /* put our clear routine into place */
  87. }
  88.     
  89. sketdeactivate()        /* Attached to sketch's deactivate */
  90. {                /* Enable the whole sketch menu */
  91.   menu ("Sketch", "", itemdisable);
  92.   menu ("Edit", "Clear", nop);    /* take off our clear routine */
  93. }
  94.  
  95. sketch(x,y)            /* follow mouse while it's down */
  96. int x,y;            /* we will get passed the mouse Point */
  97. {
  98.   Point xy, lastxy;        /* points of interest */
  99.   WindowPtr ourport;
  100.   
  101.   GetPort (&ourport);
  102.   PenSize (ourpensize, ourpensize);
  103.   MoveTo (x, y);        /* position pen at mouse down Point */
  104.   while ( StillDown() ) {    /* while the Button is pressed */
  105.     GetMouse (&xy);        /* read the mouse position */
  106.     #ifdef MEGAMAX
  107.     if ( (xy.a.v != lastxy.a.v) || (xy.a.h != lastxy.a.h) ) {
  108.       LineTo (xy.a.h, xy.a.v);    /* if mouse moved, draw a Line to it */
  109.       lastxy.a.v = xy.a.v;    /* record position so we can detect */
  110.       lastxy.a.h = xy.a.h;    /*   a Move. */
  111.     }                /* could runruns here to have circles */
  112.     #else
  113.     if ( (xy.v != lastxy.v) || (xy.h != lastxy.h) ) {
  114.       LineTo (xy.h, xy.v);    /* if mouse moved, draw a Line to it */
  115.       lastxy.v = xy.v;        /* record position so we can detect */
  116.       lastxy.h = xy.h;        /*   a Move. */
  117.     }                /* could runruns here to have circles */
  118.     #endif
  119.     runruns(0L);
  120.     SetPort (ourport);
  121.   }
  122. }
  123.     
  124. setpensize(size)        /* Attached to pen size menus */
  125. char *size;            /* menu Item name given to us */
  126. {
  127.   static char lastsize[5]={'1','\0'};    /* keep last setting */
  128.   menu ("Sketch", lastsize, itemuncheck);    /* remove old check mark */
  129.   menu ("Sketch", size, itemcheck);    /* install a new check mark */
  130.   strcpy (lastsize, size);    /* save size for the next setpensize */
  131.   ourpensize = *size - '0';        /* convert to a number */
  132. }
  133.  
  134. /**************************/
  135. /*   The Circles Window   */
  136. /**************************/
  137.  
  138. int newabs(x)            /* an absolute value funtion */
  139. int x;
  140. {
  141.   if (x < 0)
  142.     x = -x;
  143.   return (x);
  144. }
  145.  
  146. circles()            /* draw a Random sized circle */
  147. {
  148.   static WindowPtr wind = (WindowPtr) 0; /* keep "circle" WindowPtr */
  149.   static int speed = 1;        /* current count between new circles */
  150.   register int cx, cy;        /* center of the circle */
  151.   int r, b, rd;            /* right and bottom edges */
  152.   register Pattern *color;    /* color for the circle */
  153.   Rect therect;            /* rectangle to draw circle in */
  154.   if ( --speed == 0) {        /* count, and if it reaches zero... */
  155.     speed = circspeed;        /* reset the count */
  156.     if ((long)wind == 0L)     /* get window pointer if we need */
  157.       wind = windowpoint ("Circles");  /* save window for next time */
  158.     SetPort (wind);        /* set output to the circle window */
  159.     #ifdef MEGAMAX
  160.       r = wind->portRect.a.right;    /* get current size of window */
  161.       b = wind->portRect.a.bottom;
  162.     #else
  163.       r = wind->portRect.right;    /* get current size of window */
  164.       b = wind->portRect.bottom;
  165.     #endif
  166.     cx = newabs(Random()) % (r-30) + 1;    /* pick a Random location */
  167.     cy = newabs(Random()) % (b-30) + 1;
  168.     rd = newabs(Random()) % 25 + 5;        /* and size */
  169.     SetRect ( &therect, cx, cy, cx+rd, cy+rd);    /* make a rectangle */
  170.     switch ( newabs(Random()) % 4) {        /* pick a color */
  171.       case 0: color = (Pattern *)(&dkGray); break;
  172.       case 1: color = (Pattern *)(<Gray); break;
  173.       case 2: color = (Pattern *)(&gray); break;
  174.       case 3: color = (Pattern *)(&black); break;
  175.     }
  176.     FillOval ( &therect, color);    /* make the circle */
  177.   }
  178. }
  179.  
  180. setspeed(name)                /* attached to circle's menu */
  181. char *name;
  182. {
  183.   menu ("Circles","Slow/S", itemuncheck);    /* another way to uncheck last */
  184.   menu ("Circles","Medium/M", itemuncheck); /* just uncheck everything */
  185.   menu ("Circles","Fast/F", itemuncheck);
  186.   menu ("Circles", name, itemcheck);    /* then check the current  */
  187.   if (strcmp("Slow/S", name) ==0) circspeed = 100;  /* set the reset count */
  188.   else if (strcmp("Medium/M", name) ==0) circspeed = 50;
  189.   else if (strcmp("Fast/F", name) ==0) circspeed = 1;
  190. }
  191.  
  192. /***********************************************/
  193. /*   Generate a new sketch window by command   */
  194. /***********************************************/
  195.  
  196. nwindow()            /* command to attatch to new window menu */
  197. {
  198.   static topplace = 100;    /* remember where to put next window */
  199.   char newname[255];        /* string storage for window's name */
  200.   strcpy (newname, "New Window");    /* default window name */
  201. retry:                /* prompt for the name from user */
  202.   if (prompt ("Give me a unique name for the new window:", newname)) {
  203.     if ( (long)windowpoint(newname) != 0L ) {
  204.       if (message ("Sorry, a window by that name already exists.")) 
  205.         goto retry;        /* if exists, ask for another name */
  206.     } else {
  207.       if ( strlen(newname) > 0)  {  /* if ok, make the new window */
  208.         /* then it is new */
  209.         window (newname, 20,topplace, 200,topplace+60, 
  210.       sketactivate, sketdeactivate, nop, sketch);
  211.         topplace += 5;        /* adjust top for next new window */
  212.         if (topplace > 300) topplace = 100;  /* reset top if too low */
  213.       }
  214.     }
  215.   }
  216. }
  217.  
  218. /**********************/
  219. /*  About SimpleDemo  */
  220. /**********************/
  221.  
  222. usageinfo()        /* messages for Usage Info menu choice */
  223. {
  224.   char mess[255];    /* string to form a message */
  225.   strcpy (mess,"You may use, distribute your programs which use, ");
  226.   strcat (mess,"and copy SimpleTools if you send ");
  227.   strcat (mess,"$20 or more to ...");
  228.   if (message (mess)) {  /* if OK pressed for first message */
  229.     strcpy (mess, "Erik Kilk\r4949 Snyder Lane, #247\r");
  230.     strcat (mess, "Rohnert Park, CA  94928");
  231.     if (message (mess))  /* if OK pressed for 2nd message */
  232.       message ("Thank you for your support.");  /* show final message */
  233.   }
  234. }
  235.  
  236. tellabout()        /* for the About SimpleDemo menu choice */
  237. {
  238.   char mess[255];    /* string to form a message */
  239.   strcpy (mess,"SimpleDemo -- Copyright 1986 Erik Kilk\r");
  240.   strcat (mess,"A demonstration of the use of SimpleTools");
  241.   strcat (mess," as a Macintosh toolbox aid.");
  242.   if (message (mess))    /* display the string in a dialog */
  243.     usageinfo();
  244. }
  245.   
  246. /**************************/
  247. /* The free memory window */
  248. /**************************/
  249.  
  250. writemem(forsure)            /* write the FreeMem value */
  251. int forsure;                /* true if force write */
  252. {                    /* false if only on change */
  253.   static long lastmem;
  254.   long mem;
  255.   int foo;
  256.   char thestring[10], outstr[10];
  257.   Rect erasearea;
  258.   mem = FreeMem();            /* get new free memory */
  259.   foo = mem != lastmem;            /* compare it to last reading */
  260.   if ( forsure | foo ) {        /* if forsure or different */
  261.     withwindow ("FreeMem");        /* set window for output */
  262.     home();
  263.     MoveTo (0, 12);            /* write new value */
  264.     NumToString (mem, thestring);
  265.     #ifndef MEGAMAX
  266.       PtoCstr (thestring);
  267.     #endif
  268.     strcpy (outstr, " Free ");
  269.     strcat (outstr, thestring);
  270.     #ifndef MEGAMAX
  271.       CtoPstr (outstr);
  272.     #endif
  273.     DrawString (outstr);
  274.     lastmem = mem;            /* record this for next time */
  275.   }
  276. }
  277.  
  278. showmem(windp)                /* write FreeMem on an update */
  279. WindowPtr windp;
  280. {
  281.   EndUpdate (windp);            /* allow use of entire window */
  282.   writemem(TRUE);            /* clear and write new value */
  283.   BeginUpdate (windp);
  284. }
  285.  
  286. newmemvalue()                /* write FreeMem occasionally */
  287. {
  288.   writemem(FALSE);            /* write only if value changed */
  289. }
  290.  
  291. /* This is a special, non-standard, method of moving a window.  Our
  292.    free memory window does not have a title bar.  So, I have decided
  293.    to allow it to be moved if the mouse is clicked in its content
  294.    Region.  This routine is attached to the free memory in content
  295.    procedure with SimpleTools.  SimpleTools convenienly gives us
  296.    various parameters, here we need the window pointer and the event
  297.    record.  The event record has the coordinates of the mouse in
  298.    globals */
  299.    
  300. movemem(x,y,windp,event)    /* free mem's inContent procedure */
  301. int x,y;            /* SimpleTools gives us these for */
  302. WindowPtr windp;        /* an in-content procedure */
  303. EventRecord *event;
  304. {
  305.   #ifdef MEGAMAX
  306.       DragWindow (windp, &(event->where), &drag);
  307.   #else
  308.       DragWindow (windp, event->where, &drag);
  309.   #endif
  310. }
  311.  
  312. /************************/
  313. /*   Question window    */
  314. /************************/
  315.  
  316. quest_update ()                /* repaint question window    */
  317. {
  318.     stgotoxy (0, 0);            /* top left corner textwise    */
  319.     strcpy (outstring, "Hello, ");
  320.     strcat (outstring, name);
  321.     #ifndef MEGAMAX
  322.         CtoPstr(outstring);
  323.     #endif
  324.     DrawString(outstring);
  325. }
  326.  
  327. quest_getname ()            /* get a name in quest window    */
  328. {
  329.     getlinecaps = TRUE;            /* force each word to be cap'd    */
  330.     showawindow ("Questions");        /* make window active        */
  331.     quest_update ();
  332.     stgotoxy (0, 2);            /* 3rd row, far left textwise    */
  333.     strcpy (outstring,"What is your name? ");  
  334.     #ifndef MEGAMAX
  335.         CtoPstr (outstring);
  336.     #endif
  337.     DrawString (outstring);
  338.     getline ("", name);            /* ask for name of user        */
  339.     home ();                /* erase the window        */
  340.     quest_update ();            /* repaint the window        */
  341. }
  342.  
  343. /**********************/
  344. /*   Initialization   */
  345. /**********************/
  346.  
  347. setup()        /* to be called with Start Demo menu choice */
  348. {
  349.   int temp;
  350.   strcpy (name, "World");
  351.   SetRect (&drag, 4, 24, 508, 338);  /* drag area for free mem window */
  352.   windmenu = FALSE;    /* modify default values, optional */
  353.   dogoaway = FALSE;    /* optional change */
  354.   window("Circles", 20, 55, 245, 315,nop, nop, nop, nop); /* circles */
  355.   temp = wprocid;    /* save value for an optional change */
  356.   wprocid = 3;         /* optional change to set to altDBoxProc */
  357.   window ("FreeMem", 407, 322, 502, 337, nop, nop, showmem, movemem);
  358.   wprocid = temp;    /* return to defaults */
  359.   windmenu = TRUE;    
  360.   dogoaway = TRUE;
  361.   window ("Sketch", 265, 55, 490, 315,sketactivate,sketdeactivate, 
  362.       nop, sketch);    /* display the sketch pad window */
  363.   window ("Questions", 146, 276, 368, 337, nop, nop, quest_update, nop);
  364.   menu ( applestring, "About SimpleDemo", tellabout);
  365.   menu ( applestring, "About SimpleDemo", itemenable);
  366.   menu ( "File", "New", nwindow);        /* setup File menu */
  367.   menu ( "File", "New", itemenable);
  368.   menu ( "File", "Set Name", quest_getname);
  369.   simplequits ();                /* Transfer and Quit    */
  370.   menu ("Circles", "Speed", itemdisable);    /* install circles menu */
  371.   menu ("Circles", "Slow/S", setspeed);        /* optional key equivs */
  372.   menu ("Circles", "Medium/M", setspeed);
  373.   menu ("Circles", "Fast/F", setspeed);
  374.   menu ("Circles", "Medium/M", itemcheck);
  375.   menu ("Sketch", "Pen Size", itemdisable);  /* install sketches menu */
  376.   menu ("Sketch", "", itemdisable);
  377.   menu ("Sketch", "1", setpensize);
  378.   menu ("Sketch", "1", itemcheck);
  379.   menu ("Sketch", "2", setpensize);
  380.   menu ("Sketch", "3", setpensize);
  381.   menu ("Sketch", "4", setpensize);
  382.   menu ("Sketch", "5", setpensize);
  383.   menu ("Sketch", "6", setpensize);
  384.   run (circles);        /* keep drawing circles */
  385.   run (newmemvalue);        /* check if mem changed occasionally */
  386. }
  387.  
  388. main()
  389. {
  390.   simpletools("About SimpleDemo");    /* setup Apple and Edit menus */
  391.   setup();                /* setup our program */
  392.   for (;;) simpleevents();        /* process events */
  393. }
  394.